/**
* @program: src
*
* @description:
*
* @author: Mr.chen
*
* @create: 2020-12-20 09:55
**/
package orm

import (
	"ydd-im/service/api/define"
	"ydd-im/service/dbproxy/logic/group"
	"ydd-im/util/logger"
	"ydd-im/util/response"
	"fmt"
	"strings"

	"go.uber.org/zap"
)

/**
 * 添加群，将指定的用户加入群
 * @access  public
 * @param   int     userid  用户id
 * @param   int     sid  群所属签名
 * @param   string     groupkey  群自定义id
 * @param   string     name  群名称
 * @param    []string     members  群成员
 * @return  mix
 */
func Creategroup(userid, sid int64, groupkey, name string, members string) (res define.ExecResult) {
	res.Suc = true
	res.Msg = "ok"
	res.Code = response.SuccessCode
	fmt.Println("=============",members)
	err := group.AddGroup(int(userid), int(sid), groupkey, name, strings.Split(members, ","))
	if err != nil {
		logger.GetLg().Error("创建群组失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Int("userid", int(userid)),
			zap.Int("sid", int(sid)),
			zap.String("groupkey", groupkey),
			zap.String("name", name),
			zap.Any("members", members),
			zap.Any("err", err.Error()),
		)
		res.Suc = false
		res.Msg = err.Error()
		res.Code = response.ErrorCode
		return
	}
	logger.GetLg().Info("创建群组成功",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.Int("userid", int(userid)),
		zap.Int("sid", int(sid)),
		zap.String("groupkey", groupkey),
		zap.String("name", name),
		zap.Any("members", members),
	)
	return res
}

/**
 * 加入群，将指定的用户加入群
 * @access  public
 * @param   int     userid  用户id
 * @param   int     sid  群所属签名
 * @param   string     groupkey  群自定义id
 * @param   []string     members  群成员
 * @return  mix
 */
func JoinGroup(userid, sid int64, groupkey string, members string) (res define.ExecResult) {
	res.Suc = true
	res.Msg = "ok"
	res.Code = response.SuccessCode
	err := group.JoinGroup(int(userid), int(sid), groupkey, strings.Split(members, ","))
	if err != nil {
		logger.GetLg().Error("加入群组失败",
			zap.String("host", define.LocalHost),
			zap.String("port", define.Port),
			zap.Int("userid", int(userid)),
			zap.Int("sid", int(sid)),
			zap.String("groupkey", groupkey),
			zap.Any("members", members),
			zap.Any("err", err.Error()),
		)
		res.Suc = false
		res.Msg = err.Error()
		res.Code = response.ErrorCode
		return
	}
	logger.GetLg().Info("创建群组成功",
		zap.String("host", define.LocalHost),
		zap.String("port", define.Port),
		zap.Int("userid", int(userid)),
		zap.Int("sid", int(sid)),
		zap.String("groupkey", groupkey),
		zap.Any("members", members),
	)
	return res
}
